<template>
  <div>
    <!--查询参数-->
    <params ref="params" v-show="!modifyEntity.showModify" @handleRefresh="handleRefresh" :loading="loading"></params>
    <!--表格页面-->
    <my-table ref="myTable" v-show="!modifyEntity.showModify" :dataSource="dataSource" :columns="columns"
      :loading="loading" @handleAdd="handleAdd" @removeSubmit="removeSubmit" @handleEdit="handleEdit"
      @handleImport="handleImport" @handleExport="handleExport" @handleDetail="handleDetail" @loadData="loadData"
      addAction="adminUser_create" deleteAction="adminUser_delete" editAction="adminUser_edit"
      detailAction="adminUser_details" importAction="adminUser_import" exportAction="adminUser_export">
      <template slot="isPush" slot-scope="{ text }">
        <a-tag v-if="text.record.isPush" color="#f50">开</a-tag>
        <a-tag v-if="!text.record.isPush" color="#2db7f5">关</a-tag>
      </template>

      <template slot="status" slot-scope="{ text }">
        <a-switch :checked="text.record.status" v-action:adminUser_enable @change="changeStatus(text.record)"
          un-checked-children='禁用' checked-children='启用' />
      </template>

      <template v-slot:buttons="{ item }">
        <a @click="handleResetPwd(item)" v-action:adminUser_resetPwd>重置密码</a>
      </template>
    </my-table>
    <!--修改页面-->
    <modify v-if="modifyEntity.showModify" @handleRefresh="handleRefresh" :modifyEntity="modifyEntity" />
  </div>
</template>
<script>
import modify from "./modify";
import myTable from "_c/myTable/index";
import params from "./components/params";
import {
  adminUserColumns,
  queryAdminUser,
  deleteAdminUser,
  resetPwd,
  updateStatus
} from "@/api/sys/identity/adminUser";

export default {
  name: "AdminUser",
  components: { params, modify, myTable },
  data() {
    return {
      modifyEntity: {
        // 传递的主键
        id: "",
        // 页面标题
        title: "",
        // 页面操作类型
        operation: "",
        // 是否展示编辑页面
        showModify: false,
      },
      // 加载动画
      loading: false,
      // 绑定的数据源
      dataSource: [],
      // 表格绑定的列
      columns: adminUserColumns,
    };
  },
  mounted() {
    this.loadData();
  },
  methods: {
    //#region 启用、禁用
    changeStatus(item) {
      let that = this
      updateStatus({ id: item.id, status: !item.status })
        .then((res) => {
          if (res.code==200) {
            that.$notification.success({ description: res.msg })
            that.handleRefresh()
          } else {
            that.$notification.error({ description: res.msg })
          }
        })
    },
    //#endregion
    
    handleResetPwd(item) {
      var that = this;
      this.$confirm({
        title: "确定重置" + item.nickName + "的密码吗？？",
        onOk() {
          resetPwd({ id: item.id })
            .then((res) => {
              if (res.statusCode == 200) {
                that.handleRefresh();
                that.$notification.success({ description: res.msg });
              } else {
                that.$notification.warning({ description: res.msg });
              }
            })
            .catch((error) => { });
        },
        onCancel() { },
      });
    },
    //#region 查询列表
    // 保存/修改后刷新表格
    handleRefresh() {
      this.modifyEntity.showModify = false;
      this.$refs.myTable.pagination.current = 1;
      this.loadData();
    },
    // 加载数据
    loadData() {
      this.loading = true;
      let queryParams = {
        ...this.$refs.params.params,
        current: this.$refs.myTable.pagination.current,
        pageSize: this.$refs.myTable.pagination.pageSize,
      };
      let that = this;
      queryAdminUser(queryParams)
        .then((res) => {
          let { data, totalCount } = res.data;
          that.dataSource = data;
          this.$refs.myTable.pagination.total = totalCount;
          that.loading = false;
        })
        .catch(() => {
          that.loading = false;
        });
    },
    //#endregion

    //#region 添加
    // 打开添加页面
    handleAdd() {
      this.modifyEntity = {
        title: "新增平台员工",
        showModify: true,
        operation: this.operationFlag.Add,
      };
    },
    //#endregion

    //#region 删除
    // 删除
    removeSubmit(ids) {
      deleteAdminUser(ids).then((res) => {
        if (res.code==200) {
          this.$notification.success({ description: res.msg });
          this.handleRefresh();
        } else {
          this.$notification.error({ description: res.msg });
        }
      });
    },
    //#endregion

    //#region 修改
    // 打开编辑页面
    handleEdit(item) {
      this.modifyEntity = {
        id: item.id,
        title: "修改平台员工",
        showModify: true,
        operation: this.operationFlag.Edit,
      };
    },
    //#endregion

    //#region 详情
    // 打开详情页面
    handleDetail(item) {
      this.modifyEntity = {
        id: item.id,
        title: "平台员工详情",
        showModify: true,
        operation: this.operationFlag.Detail,
      };
    },
    //#endregion

    //#region 导入
    // 导入数据
    handleImport() { },
    //#endregion

    //#region 导出
    // 导出数据
    handleExport() { },
    //#endregion
  },
};
</script>


